package rs.networx.dao;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.Query;

import rs.networx.entity.Post;
import rs.networx.dao.PersonDao;
import rs.networx.dao.PostDao;
import rs.networx.entity.Person;
@Stateless
@Local(PostDao.class)
public class PostDaoBean extends GenericDaoBean<Post, Integer> implements PostDao {
	@EJB
	PersonDao personDao;
	
	public enum PostCategory{
		
		Picture(1), Video(2), Link(3);
		
		private Integer type;
		
		PostCategory(Integer type){
			this.type = type;
		}
		
		public Integer getType(){
			return type;
		}		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Post> getLastNPosts(int n, Integer personID) {
		System.out.println("** Uzimamo poslednjih: " + n + " postova od PersonID: " + personID);

		Person person = personDao.findById(personID);
		if (person == null) {
			System.out.println("ERROR: Trazeni person ne postoji!");
			return null;
		}
		
		Query q = em.createQuery("SELECT p FROM Post p WHERE p.owner=:owner order by p.date desc");
		q.setMaxResults(n);

		q.setParameter("owner", person);


		List<Post> posts = q.getResultList();

		return posts;
	}
}
